package com.cti.ftpservice.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.cti.ftpservice.dao.OnixDataDao;
import com.cti.ftpservice.model.onix.OnixBookBaseInfo;
import com.cti.ftpservice.model.onix.OnixSupplyBookInfo;
import com.cti.ftpservice.utils.HibernateUtil;
import com.cti.ftpservice.vo.OnixBookInfoQueryVo;
import com.cti.ftpservice.vo.PageViewVO;



@Repository("onixDataDao")
public class OnixDataDaoImpl  extends FullTextSearchDao implements OnixDataDao {

	/**
	 * 保存onix书目基础信息
	 */
	public void addOrUpdateOnixBookInfo(OnixBookBaseInfo bookBaseInfo) {
		addOrUpdateObj(bookBaseInfo);
	}

	@Override
	public OnixBookBaseInfo getOnixBookBaseInfoById(String id) {
		String hql="select t from "+OnixBookBaseInfo.class.getName()+" t where t.id=:id";
		Map<String, Object> mapParams=new HashMap<String,Object>();
		mapParams.put("id", id);
		return getSingleObject(hql, mapParams);
	}
	@Override
	public OnixBookBaseInfo getOnixBookBaseInfoByIsbn(String isbn) {
		String hql="select t from "+OnixBookBaseInfo.class.getName()+" t where t.isbn=:isbn";
		Map<String, Object> mapParams=new HashMap<String,Object>();
		mapParams.put("isbn", isbn);
		return getSingleObject(hql, mapParams);
	}
	
	/**
	 * 多线程中调用的方法，慎用
	 */
	public OnixBookBaseInfo getOnixBookBaseInfoByIsbnFromMultithread(String isbn) {
		String hql="select t from "+OnixBookBaseInfo.class.getName()+" t where t.isbn='"+isbn+"'";
		Query query=HibernateUtil.getCurrentSession().createQuery(hql);
		query.setFirstResult(0);
		query.setMaxResults(1);
		return (OnixBookBaseInfo) query.uniqueResult();
	}
	
	@Override
	public OnixBookBaseInfo getOnixBookBaseInfoByIsbnAndftpFileInfoId(String isbn, String ftpFileInfoId) {
		String hql="select t from "+OnixBookBaseInfo.class.getName()+" t where t.isbn=:isbn and t.ftpFileInfo.id=:ftpFileInfoId";
		Map<String, Object> mapParams=new HashMap<String,Object>();
		mapParams.put("isbn", isbn);
		mapParams.put("ftpFileInfoId", ftpFileInfoId);
		return getSingleObject(hql, mapParams);
	}
	@Override
	public PageViewVO<OnixBookBaseInfo> getOnixBookBaseInfoVosByQuery(
			OnixBookInfoQueryVo queryVo, int pageIndex, int pageSize) {
		String hql=" from "+OnixBookBaseInfo.class.getName()+" t where 1=1";
		Map<String, Object> mapParams=new HashMap<String,Object>();
		if(queryVo!=null){
			hql+=queryVo.getHql();
			mapParams=queryVo.GetMapParama();
		}
		return getPageViewVO(hql,mapParams,pageIndex,pageSize);
	}

	@Override
	public void addOrUpdateOnixSupplyBookInfo(OnixSupplyBookInfo supplyinfo) {
		addOrUpdateObj(supplyinfo);
	}

	@Override
	public void deleteOnixSupplyBookInfo(OnixSupplyBookInfo supplyinfo) {
		deleteObj(supplyinfo);
	}

	@Override
	public void commitSession() {
		flush();
		getSession().getTransaction().commit();
	}

	@Override
	public int getParsedCountByFtpInfoId(String ftpFileInfoId) {
		String hql=" select count(t.id) from "+OnixBookBaseInfo.class.getName()+" t where t.ftpFileInfo.id=:ftpFileInfoId ";
		Map<String, Object> mapParams=new HashMap<String,Object>();
		mapParams.put("ftpFileInfoId", ftpFileInfoId);
		Object obj=getSingleObject(hql, mapParams);
		return obj!=null?Integer.parseInt(obj.toString()):0;
	}

	@Override
	public void rollbackSession() {
		getSession().getTransaction().rollback();
	}

	@Override
	public void addOnixBookInfo(OnixBookBaseInfo bookBaseInfo) {
		addObj(bookBaseInfo);
	}

	@Override
	public void addOnixSupplyBookInfo(OnixSupplyBookInfo supplyinfo) {
		addObj(supplyinfo);
	}
	
	public void rebuildIndex() {
		rebuildIndex(OnixBookBaseInfo.class);
	}
}
